package com.example.happiness.mapper;

import com.example.happiness.pojo.Order;
import com.example.happiness.pojo.adminvo.AdminOrderVO;
import com.example.happiness.pojo.employeevo.ERevenueVO;
import com.example.happiness.pojo.employeevo.EmployOrderVO;
import com.example.happiness.pojo.employeevo.EmployeeRecordVO;
import com.example.happiness.pojo.generalDTO.OrderStateDTO;
import com.example.happiness.pojo.admindto.OrdersPageDto;
import com.example.happiness.pojo.adminvo.AdminOrderPageVO;
import com.example.happiness.pojo.uservo.UserOrderVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OrderMapper {
    int deleteByPrimaryKey(String orderid);

    int insert(Order record);

    int insertSelective(Order record);

    Order selectByPrimaryKey(String orderid);

    int updateByPrimaryKeySelective(Order record);

    int updateByPrimaryKey(Order record);

    Page<AdminOrderPageVO> pageQuery(OrdersPageDto ordersPageDto);

    @Select("SELECT o.orderid, o.state, s.image, s.sname, s.price, o.orderdate, \n" +
            "o.address, o.phone, o.remark,o.createdate\n" +
            "FROM `serve` s, `order` o\n" +
            "WHERE o.sid = s.sid and o.userid = #{userid}")
    List<UserOrderVO> getuserorders(String userid);

    @Update("UPDATE `order` SET state = #{state} WHERE orderid = #{orderid}")
    void updataOrderState(OrderStateDTO orderStateDTO);

    //TODO 含常量的SQL
    @Select("select o.orderid,s.sname,o.state,o.orderdate,o.address,o.fulladdress,s.price,od.proportion from\n" +
            "`order_dispatch` od,`serve` s,`order` o where o.orderid=od.orderid and " +
            "o.sid = s.sid and od.eid = #{eid} AND o.state IN (3, 4)")
    List<EmployOrderVO> getEmployOD(String eid);

    //TODO 含常量的SQL
    @Select("SELECT\n" +
            "    s.sname,\n" +
            "    o.address,\n" +
            "    o.fulladdress,\n" +
            "    o.orderdate,\n" +
            "    s.price,\n" +
            "    od.proportion,\n" +
            "    f.score,\n" +
            "    f.content\n" +
            "FROM\n" +
            "    serve s\n" +
            "JOIN\n" +
            "    `order` o ON o.sid = s.sid\n" +
            "JOIN\n" +
            "    order_dispatch od ON o.orderid = od.orderid\n" +
            "LEFT JOIN\n" +
            "    feedback f ON o.orderid = f.orderid\n" +
            "WHERE\n" +
            "    o.state IN (5,9,10,11)\n" +
            "    AND od.eid = #{eid}")
    List<EmployeeRecordVO> getServe(String eid);

    @Select("select o.orderid,u.realname as username,s.sname,o.orderdate,o.state as ostate,o.address,o.fulladdress\n" +
            "from `order` o,user u,serve s,order_dispatch od where\n" +
            "o.orderid=od.orderid and od.eid=#{employeeid} and o.sid=s.sid and o.userid=u.userid")
    List<AdminOrderVO> getOrder(String employeeid);

    @Select("select * from `order`")
    List<Order> getOrderList();

    @Select("select o.orderdate,o.state,s.price,od.proportion from order_dispatch od,`order` o,serve s where o.orderid = od.orderid and od.eid=#{eid} and o.sid = s.sid")
    List<ERevenueVO> getOrderTime(String eid);

    @Update("UPDATE `order` SET state = #{state} WHERE orderid = #{orderid}")
    void updateOrderStateByOrderid(Order order);
}